package cj.web.login.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.web.uc.controller.BaseController;
import cj.web.uc.service.MemberService;
import cj.web.uc.service.VideoUcService;
import cj.web.yc.utils.Constants;
import cj.web.yc.utils.MD5Security;

/**
 * 登录
 * @author Administrator
 *
 */
@Path("user")
public class LoginController extends BaseController{
	@Autowired
	private MemberService memberService;
	@Autowired
	private VideoUcService videoUcService;
//	@Autowired
//	private AppTokenService appTokenService; 
	
	/**
	 * 登录页
	 * @param model
	 * @param returnPage
	 * @return
	 * @throws Exception 
	 */
	@Get("loginPage")
	public String loginPage(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		String returnPage = getCookieValueByName(request,"returnPage");
		
		if(returnPage != null && returnPage.length() > 0){
			model.add("returnPage", returnPage);
		}else{
			model.add("returnPage", "/user/myPc");
		}
		
		return "/views/login.vm";
	}
	
	/**
	 * 登录方法
	 * @param phone
	 * @param pwd
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Post("login")//phone memberId
	public String login(@Param("phone")String phone,
			@Param("pwd")String pwd,HttpServletResponse response) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(phone!=null){
			
			List<Map> reps = memberService.getMemberByPhone(phone);
			
			if(reps.size()>0 && reps.size()==1){
				String l_pwd = reps.get(0).get("pwd").toString();
				String memberId = reps.get(0).get("id").toString();
				String sex = reps.get(0).get("sex").toString();
				if(l_pwd.equals(MD5Security.md5(pwd))){
					//密码相等
					setCookie(response, "phone", phone);//电话号码
					setCookie(response, "memberId", memberId);//用户ID
					setCookie(response, "sex", sex);//性别
					//种植的Key
					setCookie(response, "_token_", MD5Security.returnSingKey(phone+memberId+KEY_TOKEN_));//种植的Key
					//Cookie中移除returnPage
					removeCookie(response, "returnPage");
				}else{
					//密码错误
					jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
					jo.put("returnCode", Constants.returnObj.returnCode.code_1);
				}
				
			}else{
				//不存在此用户
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
				jo.put("returnCode", Constants.returnObj.returnCode.code_2);
			}
			
			
		}else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 登出
	 * @param response
	 * @return
	 */
	@Get("logoutPc")
	public String logoutPc(Model model,HttpServletResponse response){
		removeCookie(response,"phone");
		removeCookie(response,"memberId");
		removeCookie(response,"_token_");
		//Cookie中移除returnPage
		removeCookie(response, "returnPage");
		return "r:myPc";
	}
	
	/**
	 * 用户中心
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("myPc")
	public String myPc(Model model, HttpServletRequest request){
		if(isLogin(request)){
			String sex = getCookieValueByName(request, "sex");
			String phone = getCookieValueByName(request, "phone");
			String memberId = getCookieValueByName(request, "memberId");
			//观看历史
			List<Map> watchHistorylis = videoUcService.queryWatchHistory(Integer.parseInt(memberId), 0, 4);
			model.add("watchHistorylis", watchHistorylis);
			model.add("isLogin", true);
		}else{
			model.add("isLogin", false);
		}
		return "/views/uc/userCenter.vm";
	}
}